package com.rc.evcharger.service.rc;

import com.rc.evcharger.common.constant.level.UserLevelConstant;
import com.rc.evcharger.component.UidGenerator;
import com.rc.evcharger.mapper.rc.RcEnterpriseGainGradeRecordMapper;
import com.rc.evcharger.mapper.rc.RcEnterpriseLevelSettingMapper;
import com.rc.evcharger.mapper.rc.RcMemberFlagMapper;
import com.rc.evcharger.model.order.OrderDiscountRecord;
import com.rc.evcharger.model.order.OrderInfo;
import com.rc.evcharger.model.rc.RcEnterpriseGainGradeRecord;
import com.rc.evcharger.model.rc.RcEnterpriseGainGradeRecordExample;
import com.rc.evcharger.model.rc.RcEnterpriseLevelSetting;
import com.rc.evcharger.model.rc.RcMemberFlag;
import com.rc.evcharger.vo.cost.UserLevelPriceResult;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RcEnterpriseGainGradeRecordServiceImpl implements RcEnterpriseGainGradeRecordService {
    private static Logger logger = LogManager.getLogger(RcEnterpriseGainGradeRecordServiceImpl.class);
    @Autowired
    private RcEnterpriseGainGradeRecordMapper rcEnterpriseGainGradeRecordMapper;

    @Autowired
    private RcEnterpriseLevelSettingMapper rcEnterpriseLevelSettingMapper;

    @Autowired
    private UidGenerator uidGenerator;

    @Autowired
    private RcMemberFlagMapper rcMemberFlagMapper;


    @Override
    public RcEnterpriseGainGradeRecordMapper getMapper() {
        return rcEnterpriseGainGradeRecordMapper;
    }

    @Override
    public List<RcEnterpriseGainGradeRecord> listCurrentByMemberId(Long memberId) {
        RcEnterpriseGainGradeRecordExample example = new RcEnterpriseGainGradeRecordExample();
        example.createCriteria().andMemberIdEqualTo(memberId);
        example.setOrderByClause("created_time desc");
        return rcEnterpriseGainGradeRecordMapper.selectByExample(example);
    }

    @Override
    public RcEnterpriseGainGradeRecord selectByEnterpriseMemberId(Long enterpriseId, BigDecimal allEnterpriseRechargeAmount) {
        RcEnterpriseGainGradeRecordExample example = new RcEnterpriseGainGradeRecordExample();
        example.createCriteria().andEnterpriseIdEqualTo(enterpriseId)
                .andRechargeAmountLessThanOrEqualTo(allEnterpriseRechargeAmount);
        example.setOrderByClause("recharge_amount desc limit 1");
        List<RcEnterpriseGainGradeRecord> rcEnterpriseGainGradeRecords = rcEnterpriseGainGradeRecordMapper.selectByExample(example);
        if (rcEnterpriseGainGradeRecords.size() == 0) {
            return null;
        }
        return rcEnterpriseGainGradeRecords.get(0);
    }

    @Override
    public UserLevelPriceResult updateOrderInfo(OrderInfo orderInfo, Long enterPriseId, BigDecimal allEnterpriseRechargeAmount) {

        UserLevelPriceResult userLevelPriceResult = UserLevelPriceResult.emptyResult();
        List<OrderDiscountRecord> orderDiscountRecords0 = new ArrayList<>();

        if (orderInfo.getPayAmount().compareTo(BigDecimal.ZERO) <= 0) {
            return userLevelPriceResult;
        }

        RcMemberFlag rcMemberFlag = rcMemberFlagMapper.selectByPrimaryKey(orderInfo.getMemberId());
//        if ("1".equals(rcMemberFlag.getEnterpriseGiveStatus())) {
//            logger.warn("企业会员权益已停用，无法享受折扣，对应用户ID:{}", rcMemberFlag.getMemberId());
//            return userLevelPriceResult;
//        }

        RcEnterpriseLevelSetting rcEnterpriseLevelSetting = rcEnterpriseLevelSettingMapper.selectByEnterpriseLevel(rcMemberFlag.getEnterpriseLevel());
        if (rcEnterpriseLevelSetting == null || rcEnterpriseLevelSetting.getDiscountAmount().compareTo(BigDecimal.ZERO) == 0) {
            return userLevelPriceResult;
        }

        BigDecimal discountAmount = orderInfo.getPayAmount().subtract(orderInfo.getPayAmount().multiply(rcEnterpriseLevelSetting.getDiscountAmount()).setScale(2, BigDecimal.ROUND_HALF_UP));
        orderInfo.setDiscountAmount(orderInfo.getDiscountAmount().add(discountAmount));
        orderInfo.setPayAmount(orderInfo.getPayAmount().subtract(discountAmount));
        orderInfo.setLevelDiscount(rcEnterpriseLevelSetting.getDiscountAmount().multiply(BigDecimal.TEN));

        //添加优惠记录
        OrderDiscountRecord orderDiscount = new OrderDiscountRecord();
        orderDiscount.setTenantCode(orderInfo.getTenantCode());
        orderDiscount.setOrderId(orderInfo.getOrderId());
//        orderDiscount.setDiscountId(gainGradeRecord.getEnterpriseGainGradeRecordId());
        orderDiscount.setDiscountNum(uidGenerator.genDiscountOrderNum("Y"));
        orderDiscount.setCreatedTime(new Date());
        orderDiscount.setDiscountName("企业权益充值折扣");
        orderDiscount.setDiscountAmount(discountAmount);
        orderDiscount.setDiscountType(UserLevelConstant.RC_ENTERPRISE_RECHARGE_DISCOUNT + "");
//        orderDiscountRecordMapper.insertSelective(orderDiscount);
        orderDiscountRecords0.add(orderDiscount);

        //保存优惠记录
        userLevelPriceResult.setOrderDiscountRecords(orderDiscountRecords0);

        return userLevelPriceResult;
    }
}
